package com.zzjson.cloneable;

import com.zzjson.BufferSlice;
import lombok.Data;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2020 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.cloneable</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2020年02月25日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
@Data
public class CloneTest implements Cloneable {
	private List<String> list;
	private BufferSlice bufferSlice;

	public static void main(String[] args) throws CloneNotSupportedException {
		/*String[] original = new String[11];
		original[0] = "hello";


		String[] ts = copyOf(original, 16, String[].class);
		System.out.println(ts[0] == original[0]);*/
		CloneTest cloneTest = new CloneTest();
		cloneTest.setList(Arrays.asList("ds", "da"));
		cloneTest.setBufferSlice(new BufferSlice());
		CloneTest clone = (CloneTest) cloneTest.clone();
		// clone.getList().set(0, "fuck");
		System.out.println(cloneTest);
		System.out.println(clone);

		System.out.println(cloneTest.getList() == clone.getList());
	}

	@Override
	protected CloneTest clone() throws CloneNotSupportedException {
		Object oo = super.clone();
		CloneTest cc = (CloneTest) oo;
		cc.setList(new ArrayList<>(this.getList()));
		return cc;
	}

	public static <T, String> T[] copyOf(String[] original, int newLength, Class<? extends T[]> newType) {
		@SuppressWarnings("unchecked")
		T[] copy = ((Object) newType == (Object) Object[].class)
				? (T[]) new Object[newLength]
				: (T[]) Array.newInstance(newType.getComponentType(), newLength);
		System.arraycopy(original, 0, copy, 0,
				Math.min(original.length, newLength));
		return copy;
	}
}
